package com.qby.project.service.impl;

import com.alibaba.fastjson.JSONException;
import com.qby.project.service.QnyFileUploadService;
import com.qiniu.api.auth.AuthException;
import com.qiniu.api.auth.digest.Mac;
import com.qiniu.api.config.Config;
import com.qiniu.api.io.IoApi;
import com.qiniu.api.io.PutExtra;
import com.qiniu.api.io.PutRet;
import com.qiniu.api.rs.PutPolicy;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @description: 七牛云实现类
 * @author: qiu bo yang
 * @create: 2020-04-17 15:01
 */
@Service
public class QnyFileUploadServiceImpl implements QnyFileUploadService {
    @Value("${qiniu.file.accessKey}")
    private String accessKey;

    @Value("${qiniu.file.secretKey}")
    private String secretKey;

    @Value("${qiniu.file.bucket}")
    private String bucket;

    @Value("${file.base.url}")
    private String fileUrl;

    @Override
    public String getFileBaseUrl() {
        return fileUrl;
    }

    @Override
    public String getToken() {
        //公钥
        Config.ACCESS_KEY = accessKey;
        //私钥
        Config.SECRET_KEY = secretKey;
        Mac mac = new Mac(Config.ACCESS_KEY, Config.SECRET_KEY);
        //请确保该bucket已存在
        String bucketName = bucket;
        PutPolicy putPolicy = new PutPolicy(bucketName);
        String token = null;
        try {
            token = putPolicy.token(mac);
        } catch (AuthException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (org.json.JSONException e) {
            e.printStackTrace();
        }
        return token;
    }

    /**
     * 七牛云上传文件
     *
     * @param uploadFile
     * @param key
     * @return
     * @throws Exception
     */
    @Override
    public String uploadFile(MultipartFile uploadFile, String key) throws Exception {
        Config.ACCESS_KEY = accessKey;
        Config.SECRET_KEY = secretKey;
        Mac mac = new Mac(Config.ACCESS_KEY, Config.SECRET_KEY);
        String bucketName = bucket;
        PutPolicy putPolicy = new PutPolicy(bucketName);
        String token = putPolicy.token(mac);
        PutExtra extra = new PutExtra();
        PutRet ret = IoApi.Put(token, key, uploadFile.getInputStream(), extra);
        return key;
    }

    /**
     * 七牛云图片上传
     *
     * @param file      上传文件
     * @param imageType 图片类型
     * @return
     */
    @Override
    public String qnyUploadImage(MultipartFile file, String imageType) throws Exception {
        String fileName = file.getOriginalFilename();
        String fileType = fileName.substring(fileName.lastIndexOf("."));
        String key = generalDir(imageType) + "/" + System.currentTimeMillis() + "." + fileType;
        return getFileBaseUrl() + uploadFile(file, key);
    }

    /**
     * 图片所属分类
     *
     * @param type
     * @return
     */
    public static String generalDir(String type) {
        String dir = "default";
        if (StringUtils.isNotEmpty(type)) {
            dir = type;
        }
        return dir;
    }
}
